package control;

import control.bean.ControlBeanConstant;
import control.bean.ControlPropertiesBean;
import org.jdesktop.swingx.JXDatePicker;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;

/**
 * Created with IntelliJ IDEA.
 * User: Deshun
 * Date: 2/20/13
 * Time: 1:24 PM
 * To change this template use File | Settings | File Templates.
 */
public class DatePicker extends Control {

    private JXDatePicker datePicker;

    public DatePicker() {
        fieldType = ControlBeanConstant.DATEPICKER;
        buildControlPanel();
        buildPropertiesPanel();
    }

    @Override
    public Component buildControlPanel() {
        controlPanel = new JPanel(new GridBagLayout());
        controlPanel.setOpaque(false);

        GridBagConstraints constraints = new GridBagConstraints();

        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.ipadx = 130;
        constraints.insets = new Insets(5, 5, 5, 5);
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;

        label = new JTextField();
        controlPanel.add(label, constraints);

        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.insets = new Insets(5, 0, 0, 5);
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;

        datePicker = new JXDatePicker();
        controlPanel.add(datePicker, constraints);

        controlPanel.addFocusListener(new ControlFocusAdaptor());
        controlPanel.addMouseListener(new ControlMouseAdaptor());

        label.addFocusListener(new ControlFocusAdaptor());
        label.addMouseListener(new ControlMouseAdaptor());


        return controlPanel;
    }

    @Override
    public Component buildPropertiesPanel() {
        propertiesPanel = new JPanel(new GridBagLayout());

        fieldProperties = new JLabel("Field Properties");

        controlLabel    = new JLabel("Control Name");
        controlName     = new JTextField();

        hint            = new JCheckBox("Hint");
        required        = new JCheckBox("Required");
        readOnly        = new JCheckBox("Read Only");

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 1;
//        constraints.ipadx = 130;
        constraints.ipady = 14;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(fieldProperties, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.ipadx = 0;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(controlLabel, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        //constraints.gridheight = 2;
//        constraints.ipadx = 114;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(5, 0, 0, 0);
        propertiesPanel.add(controlName, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 3;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(hint, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 4;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(1, 0, 0, 0);
        propertiesPanel.add(required, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 5;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(1, 0, 0, 0);
        propertiesPanel.add(readOnly, constraints);

        Component strut = Box.createHorizontalStrut(160);
        constraints.gridy = 6;
        propertiesPanel.add(strut, constraints);

        return propertiesPanel;
    }

    public ControlPropertiesBean exportData() {
        super.exportData();

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat();
            controlPropertiesBean.addProperty(ControlBeanConstant.DATEVALUE, dateFormat.format(datePicker.getDate()));
        } catch ( Exception e ) {
            System.out.println("date picker value not set");
        }

        return controlPropertiesBean;
    }

    public void importData(ControlPropertiesBean controlPropertiesBean) {
        try {
            super.importData(controlPropertiesBean);
            SimpleDateFormat dateFormat = new SimpleDateFormat();
            datePicker.setDate(dateFormat.parse(controlPropertiesBean.getProperty(ControlBeanConstant.DATEVALUE)));
        } catch ( Exception e ) {

        }
    }
}
